<template>
  <div class="hello">
    <div>===================这里是组件1 start==================</div>
    组件2状态机=={{ key2 }}
    <button @click="handleAdd">更新key1</button>
    <div>===================这里是组件1 end==================</div>
  </div>
</template>

<script>
import { sharedState, subscribe } from "@/utils/SharedState";
export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return {
      key2: 0,
    };
  },
  watch: {
    sharedState(val) {
      console.log("key2更新了", val);
    },
  },
  created() {
    sharedState.key1 = { a: 1 };
    subscribe("key2", () => {
      this.key2 = sharedState.key2;
    });
  },
  methods: {
    handleAdd() {
      sharedState.key1.a += 1;
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
